package com.zyt.controller;

import cn.hutool.db.Session;
import com.zyt.entity.Book;
import com.zyt.entity.CarItem;
import com.zyt.entity.Cart;
import com.zyt.service.AddressService;
import com.zyt.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;

@Controller
public class CartController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/cart")
    public String addCart(Integer bookId, HttpSession session){
        Cart cart =(Cart) session.getAttribute("cart");
        if (cart==null){
            cart=new Cart();
            session.setAttribute("cart", cart);
        }

        Map<Integer, CarItem> buyMap=cart.getBuyMap();
        if (buyMap.containsKey(bookId)){
            CarItem carItem= buyMap.get(bookId);
            carItem.setCount(carItem.getCount()+1);

        }else {
            CarItem carItem=new CarItem();
            Book book =bookService.selectById(bookId);
            carItem.setBook(book);
            carItem.setCount(1);
            buyMap.put(bookId,carItem);
        }
        Double totalMoney=0.0;
        Double saveMone=0.0;
        Set<Map.Entry<Integer,CarItem>> entries=buyMap.entrySet();
        for (Map.Entry<Integer,CarItem> entry:entries) {
            Double dangPrice =entry.getValue().getBook().getDangprice();
            Integer cont =entry.getValue().getCount();
            Double bookPrice =entry.getValue().getBook().getBookprice();

            totalMoney += dangPrice * cont;
            saveMone +=(bookPrice-dangPrice)*cont;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMone(saveMone);

        System.out.println(totalMoney);
        System.out.println(saveMone);
        return "redirect:/cart/cart_list.jsp";
    }
    @RequestMapping("/removeCart")
    public String removeCart(Integer bookId,HttpSession session){
        Cart cart =(Cart) session.getAttribute("cart");
        Map<Integer,CarItem> buyMap=cart.getBuyMap();
        CarItem carItem=buyMap.get(bookId);
        buyMap.remove(bookId);
        Map<Integer,CarItem> removeMap=cart.getRemoveMap();
        removeMap.put(bookId,carItem);

        Double totalMoney=0.0;
        Double saveMone=0.0;
        Set<Map.Entry<Integer,CarItem>> entries=buyMap.entrySet();
        for (Map.Entry<Integer,CarItem> entry:entries) {
            Double dangPrice =entry.getValue().getBook().getDangprice();
            Integer cont =entry.getValue().getCount();
            Double bookPrice =entry.getValue().getBook().getBookprice();

            totalMoney += dangPrice * cont;
            saveMone +=(bookPrice-dangPrice)*cont;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMone(saveMone);

        System.out.println(totalMoney);
        System.out.println(saveMone);
        return "redirect:/cart/cart_list.jsp";
    }

    @RequestMapping("/recoverCart")
    public String recoverCart(Integer bookId,HttpSession session){
        Cart cart =(Cart) session.getAttribute("cart");
        Map<Integer,CarItem> removeMap=cart.getRemoveMap();

        CarItem carItem=removeMap.get(bookId);
        removeMap.remove(bookId);
        Map<Integer,CarItem> buyMap=cart.getBuyMap();
        buyMap.put(bookId,carItem);

        Double totalMoney=0.0;
        Double saveMone=0.0;
        Set<Map.Entry<Integer,CarItem>> entries=buyMap.entrySet();
        for (Map.Entry<Integer,CarItem> entry:entries) {
            Double dangPrice =entry.getValue().getBook().getDangprice();
            Integer cont =entry.getValue().getCount();
            Double bookPrice =entry.getValue().getBook().getBookprice();

            totalMoney += dangPrice * cont;
            saveMone +=(bookPrice-dangPrice)*cont;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMone(saveMone);

        System.out.println(totalMoney);
        System.out.println(saveMone);
        return "redirect:/cart/cart_list.jsp";
    }
    @RequestMapping("/updateCart")
    public String updateCart(int bookId,int count,HttpSession session){
        Cart cart =(Cart) session.getAttribute("cart");
        Map<Integer,CarItem> buyMap=cart.getBuyMap();
        CarItem carItem=buyMap.get(bookId);
        carItem.setCount(count);

        Double totalMoney=0.0;
        Double saveMone=0.0;
        Set<Map.Entry<Integer,CarItem>> entries=buyMap.entrySet();
        for (Map.Entry<Integer,CarItem> entry:entries) {
            Double dangPrice =entry.getValue().getBook().getDangprice();
            Integer cont =entry.getValue().getCount();
            Double bookPrice =entry.getValue().getBook().getBookprice();

            totalMoney += dangPrice * cont;
            saveMone +=(bookPrice-dangPrice)*cont;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMone(saveMone);

        System.out.println(totalMoney);
        System.out.println(saveMone);
        return "redirect:/cart/cart_list.jsp";


    }

}
